package com.niiwoo.civet.trade.utils;

import com.niiwoo.civet.trade.constant.DatePatternConstant;
import com.niiwoo.civet.trade.dao.entity.ProjectBorrowerSummary;
import com.niiwoo.civet.trade.enums.ProjectSubStatusEnum;
import com.niiwoo.civet.trade.enums.RepayStatusEnum;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.joda.time.DateTime;
import org.joda.time.Days;

import java.text.ParseException;
import java.util.Date;

/**
 * Created by bincun on 2017/11/24.
 */
public class RepaymentUtil {
    //计算是否逾期,坏账,正常还款

    /**
     * 使用FeeDetailLocalService.correctRepayStatus(refundDate,projectId)来替代
     * @param titularRefundDate 名义还款日
     * @return
     */
    public static RepayStatusEnum getRepayStatus(Date titularRefundDate, Date currentDate) {
        DateTime today = new DateTime(FastDateFormat.getInstance(DatePatternConstant.YYYY_MM_DD).format(currentDate));
        DateTime refundDateTime = new DateTime(titularRefundDate);
        int temp = getOverdueDay(titularRefundDate, currentDate);
        //同一天,正常还款
        if (temp <= 0) {
            return RepayStatusEnum.NORMAL;
        } else {
            refundDateTime = refundDateTime.plusMonths(1);
            temp = Days.daysBetween(refundDateTime, today).getDays();
            //超过一个月,坏账
            if (temp > 0) {
                return RepayStatusEnum.BADLOANS;
            } else {
                //小于一个月,逾期还款
                return RepayStatusEnum.OVERDUE;
            }
        }
    }

    /**
     * 计算逾期天数
     * (可以处理逾期一天，状态为正常的场景，用来取代RepaymentUtil.getRepayStatus(refundDate))
     * @param refundDate
     * @return 返回逾期天数，会查询数据库
     */
    public static int correctRepayOverdueDays(Date refundDate,ProjectSubStatusEnum projectSubStatusEnum){
        int temp = RepaymentUtil.getOverdueDay(refundDate,new Date());
        if (temp<=0 || temp>1){
            return temp;
        }else{//逾期天数为一天时
            if (projectSubStatusEnum.getSubStatus() == ProjectSubStatusEnum.REPAYING_INIT.getSubStatus()){
                return 0;
            }else {
                return 1;
            }
        }
    }

    /**
     * 计算逾期天数 (应还款日-当天时间)
     * @param refundDate
     * @return
     */
    public static Integer getOverdueDay(Date refundDate){
    	DateTime today = new DateTime(FastDateFormat.getInstance(DatePatternConstant.YYYY_MM_DD).format(DateTime.now().toDate()));
        DateTime refundDateTime = new DateTime(refundDate);
        return  Days.daysBetween(refundDateTime, today).getDays();
    }


    /**
     * 计算逾期天数 (应还款日-指定时间)
     * @param refundDate
     * @return
     */
    public static Integer getOverdueDay(Date refundDate,Date endDate){
        DateTime refundDate2 = new DateTime(FastDateFormat.getInstance(DatePatternConstant.YYYY_MM_DD).format(refundDate));
        DateTime eDate = new DateTime(FastDateFormat.getInstance(DatePatternConstant.YYYY_MM_DD).format(endDate));
        DateTime refundDateTime = new DateTime(refundDate2);
        return  Days.daysBetween(refundDateTime, eDate).getDays();
    }


    /**
     * 修改当前期数据值
     * @param summary
     * @return
     */
    public static ProjectBorrowerSummary getCorrectSummary(ProjectBorrowerSummary summary){
        DateTime today = new DateTime(FastDateFormat.getInstance(DatePatternConstant.YYYY_MM_DD).format(DateTime.now().toDate()));

        //当期已还，Summary当期指向下一期
        if(summary.getPreviousRefundDate()!=null && summary.getCurrentPeriod()>1 && RepaymentUtil.getOverdueDay(today.toDate(),summary.getPreviousRefundDate())>=0){
            //log.info("当前期{}已提前一个月进入",summary.getCurrentPeriod());
            summary.setCurrentPeriod(summary.getCurrentPeriod()-1);
            summary.setRefundDate(summary.getPreviousRefundDate());
        }
        return summary;
    }

    /**
     * 修改当前期数据值
     * @param summary
     * @return
     */
    public static ProjectBorrowerSummary getCorrectSummary(ProjectBorrowerSummary summary,Date today){

        //当期已还，Summary当期指向下一期
        if(summary.getPreviousRefundDate()!=null && summary.getCurrentPeriod()>1 && RepaymentUtil.getOverdueDay(today,summary.getPreviousRefundDate())>=0){
            //log.info("当前期{}已提前一个月进入",summary.getCurrentPeriod());
            summary.setCurrentPeriod(summary.getCurrentPeriod()-1);
            summary.setRefundDate(summary.getPreviousRefundDate());
        }
        return summary;
    }

    public static void main(String[] args) throws ParseException {
        Date date = new DateTime(new Date()).plusDays(-1).toDate();

        int i = RepaymentUtil.getOverdueDay(DateUtils.parseDate("2017-12-27","yyyy-MM-dd"),date);

        System.out.println("逾期"+i);
        System.out.println(FastDateFormat.getInstance(DatePatternConstant.YYYY_MM_DD).format(date));

    }
}
